//
//  ContentView.swift
//  xx
//
//  Created by z on 2020/6/12.
//  Copyright © 2020 z. All rights reserved.
//

import SwiftUI
import AVFoundation



func vv()
{
    let directoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
    let fileURL = URL(fileURLWithPath: "myFile", relativeTo: directoryURL).appendingPathExtension("txt")
    let myString = "Saving data with FileManager is easy!"
    guard let data = myString.data(using: .utf8) else {
        print("Unable to convert string to data")
        return
    }
    do {
        try data.write(to: fileURL)
        print("File saved: \(fileURL.absoluteURL)")
    } catch {
        // Catch any errors
        print(error.localizedDescription)
    }
    do {
        // Get the saved data
        let savedData = try Data(contentsOf: fileURL)
        // Convert the data back into a string
        if let savedString = String(data: savedData, encoding: .utf8) {
            print(savedString)
        }
    } catch {
        // Catch any errors
        print("Unable to read the file")
    }
}
class ContentViewModel: ObservableObject {
    @Published var textValue: String = "Hello"
    @Published var enteredTextValue: String = "" {
        didSet {
            print(enteredTextValue)
            
        }
    }
    @Published var aa: Bool = true {
        didSet {
            print(aa)
            
        }
    }
    @Published var textsMatch: Bool = false
}

func main()
{
     iniHelp()
   var p = [Int]()
      for i in 10000..<10050
      {
          p.append(i)
      }
    var c = cli(p)
  
    print(c.dataPort,c.numPort)
    c.doAccept()
    while true
    {
        c.work()
    }

}
struct ContentView: View {
    @ObservedObject var viewModel = ContentViewModel()
    @State var  start=0;
    @State var te=""
    @State var name=""
    @State var rGuess: Double=0.5
    @State private var showGreeting = true
    var backgroundTask = BackgroundTask()
    func checkIfTextsMatch(changed: Bool) {
        print("chch \(rGuess)")
    }
    
    var body: some View {
        VStack {
            Toggle(isOn: $viewModel.aa) {
                Text("Show welcome message")
            }
            ScrollView{
                Text("\(te)")
            }
            TextField("Enter your name", text: $viewModel.enteredTextValue)
            Slider(value: $rGuess,  onEditingChanged: self.checkIfTextsMatch)
            
            Button(action: {
                if self.start==1
                {
                    return;
                }
                else
                {
                    self.te="wait"
                    self.start=1;
                    DispatchQueue.global   (qos: .utility).async { main()}
                  self.backgroundTask.startBackgroundTask()
                    let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in
                        self.te = temp
                    }
                }
            })
            {
                Text("button")
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
